#!/bin/sh

# old distros are missing "truncate", emulate it with "dd" if necessary
do_truncate()
{
	[ "$1" = "-s" ] && size=$2 && shift 2

	if ! truncate -s $size $1 >> $LOG 2>&1 ; then
		dd if=/dev/zero of=$1 bs=1 count=0 seek=$size >> $LOG 2>&1
	fi
}

resize_test () {
DBG_FLAGS=63

echo $test_description starting > $LOG
rm -f $TMPFILE
touch $TMPFILE

# Verify that the $TMP filesystem handles $SIZE_2 sparse files.
# If that fails, try the local filesystem instead.
if do_truncate -s $SIZE_2 $TMPFILE 2> /dev/null; then
	echo "using $TMPFILE" >> $LOG
else
	rm $TMPFILE
	export TMPFILE=$(mktemp ./$test_name.tmp.XXXXXX)
	touch $TMPFILE
	echo "using $TMPFILE" >> $LOG
	if ! do_truncate -s $SIZE_2 $TMPFILE >> $LOG 2>&1; then
		rm $TMPFILE
		return 111
	fi
fi
> $TMPFILE

echo $MKE2FS $FEATURES -qF $TMPFILE $SIZE_1 >> $LOG
$MKE2FS $FEATURES -qF $TMPFILE $SIZE_1 >> $LOG

OUT_TMP=$(mktemp ${TMPDIR:-/tmp}/csum-tmp.XXXXXX)

date > $OUT_TMP
cat $E2FSCK >> $OUT_TMP
echo $CRCSUM $OUT_TMP >> $LOG 2>&1
CSUM_1=$($CRCSUM $OUT_TMP)
echo Checksum is $CSUM_1 >> $LOG

echo Setting up file system >> $LOG
$DEBUGFS -w $TMPFILE  >> $LOG 2>&1 << EOF
mkdir test
cd test
write $OUT_TMP e2fsck
ls /test
stat /test/e2fsck
quit
EOF
echo " " >> $LOG

rm -f $OUT_TMP

echo $FSCK -fy $TMPFILE >> $LOG 2>&1 
$FSCK -fy $TMPFILE >> $LOG 2>&1 

echo $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS $TMPFILE $SIZE_2 >> $LOG 2>&1
if ! $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS $TMPFILE $SIZE_2 >> $LOG 2>&1
then
	return 1
fi

echo $FSCK -fy $TMPFILE >> $LOG 2>&1
if ! $FSCK -fy $TMPFILE >> $LOG 2>&1
then
	$DUMPE2FS $TMPFILE >> $LOG
	return 1
fi

echo $DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1
$DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1

echo $CRCSUM $OUT_TMP >> $LOG 2>&1
CSUM_2=$($CRCSUM $OUT_TMP)
echo Checksum is $CSUM_2 >> $LOG

if test "$CSUM_1" != "$CSUM_2"
then
	return 1
fi

# Uncomment to grab extra debugging image
#
#mv $TMPFILE /tmp/foo.img
#return 0

echo $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
if ! $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
then
	return 1
fi

echo $FSCK -fy $TMPFILE >> $LOG 2>&1
if ! $FSCK -fy $TMPFILE >> $LOG 2>&1
then
	$DUMPE2FS $TMPFILE >> $LOG
	return 1
fi

echo $DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1
$DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1

echo $CRCSUM $OUT_TMP >> $LOG 2>&1
CSUM_2=$($CRCSUM $OUT_TMP)
echo Checksum is $CSUM_2 >> $LOG

if test "$CSUM_1" != "$CSUM_2"
then
	return 1
fi

echo $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
if ! $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
then
	return 1
fi

echo $FSCK -fy $TMPFILE >> $LOG 2>&1
if ! $FSCK -fy $TMPFILE >> $LOG 2>&1
then
	$DUMPE2FS $TMPFILE >> $LOG
	return 1
fi

echo $DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1
$DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1

echo $CRCSUM $OUT_TMP >> $LOG 2>&1
CSUM_2=$($CRCSUM $OUT_TMP)
echo Checksum is $CSUM_2 >> $LOG

if test "$CSUM_1" != "$CSUM_2"
then
	return 1
fi

echo $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
if ! $RESIZE2FS $RESIZE2FS_OPTS -d $DBG_FLAGS -M $TMPFILE $SIZE_2 >> $LOG 2>&1
then
	return 1
fi

echo $FSCK -fy $TMPFILE >> $LOG 2>&1
if ! $FSCK -fy $TMPFILE >> $LOG 2>&1
then
	$DUMPE2FS $TMPFILE >> $LOG
	return 1
fi

echo $DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1
$DEBUGFS -R "dump /test/e2fsck $OUT_TMP" $TMPFILE >> $LOG 2>&1

echo $CRCSUM $OUT_TMP >> $LOG 2>&1
CSUM_2=$($CRCSUM $OUT_TMP)
echo Checksum is $CSUM_2 >> $LOG

rm $OUT_TMP
unset OUT_TMP

rm -f $TMPFILE

if test "$CSUM_1" != "$CSUM_2"
then
	return 1
fi

return 0

}
